/*
 * @lc app=leetcode.cn id=997 lang=javascript
 *
 * [997] 找到小镇的法官
 */

// @lc code=start
/**
 * @param {number} n
 * @param {number[][]} trust
 * @return {number}
 */
var findJudge = function (n, trust) {
  if (n === 1) return 1;
  const map = {};
  const common = new Set();
  trust.map(([a, b]) => {
    map[b] = map[b] ? map[b] + 1 : 1;
    common.add(a);
  });
  const res = Object.entries(map).filter(([k, v]) => v === n - 1 && !common.has(+k));
  return res.length === 0 ? -1 : res[0][0];
};
// @lc code=end
